//
//  OrderExtFooterView.m
//  HAODE
//
//  Created by panquanReabam.com on 16/5/18.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "OrderExtFooterView.h"

@interface OrderExtFooterView()<UITextFieldDelegate,UITextViewDelegate>
@end

@implementation OrderExtFooterView

{
    NSMutableArray*sumPrices;
    UILabel *label;
    UIImageView*im;
    UIButton* ToObtain;
    UIView*myToolBar;
}
@synthesize mDelegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        myToolBar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, viewheight)];
        myToolBar.backgroundColor=jianpancolor;
        UIButton*  tubtnDone=[[UIButton alloc]initWithFrame:CGRectMake(logoX,logoY, logoheight, logowidth)];
        [tubtnDone setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [tubtnDone addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
        [myToolBar addSubview:tubtnDone];
        UIButton*btnDone=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width - 55, 12, 40, 16)];
        [btnDone setTitle:@"确定" forState:UIControlStateNormal];
        [btnDone setTitleColor:jianpancolor1 forState:UIControlStateNormal];
        btnDone .titleLabel .font=[UIFont systemFontOfSize:keyboardFot];
        [btnDone addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
        [myToolBar addSubview:btnDone];
        myToolBar.alpha=alp;
        UIButton* dabtn=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width-60, 0, 60, 40)];
        [dabtn addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
        [myToolBar addSubview:dabtn];
       
        _discountView=[[UIView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, 50)];
        _discountView.backgroundColor=[UIColor whiteColor];
        [self addSubview:_discountView];
        UILabel *lineLabel12=[[UILabel alloc] initWithFrame:CGRectMake(10, 95-0.40, ScreenWidth-20, 0.40)];
        lineLabel12.backgroundColor=qianhuiColor;
        [_discountView addSubview:lineLabel12];
        UIImageView *_xianview = [[UIImageView alloc]initWithFrame:CGRectMake(10,0, ScreenWidth-20, 0.5)];
        _xianview.image=[UIImage imageNamed:@"hengxian"];
        [_discountView addSubview:_xianview];

        UILabel *lineLabel13=[[UILabel alloc] initWithFrame:CGRectMake(10, 45, ScreenWidth-20, 0.40)];
        lineLabel13.backgroundColor=qianhuiColor;
        [_discountView addSubview:lineLabel13];
        
        _switchLabvel=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 80, 50)];
        _switchLabvel.text=@"是否配送";
        _switchLabvel.textColor=qianheiColor;
        _switchLabvel.font=[UIFont systemFontOfSize:15];
        [_discountView addSubview:_switchLabvel];
        _switchs = [[UISwitch alloc] initWithFrame:CGRectMake(ScreenWidth-75, 53-45, 55, ScreenHeight*15/1334)];
        _switchs.onTintColor=luseColor;
        _switchs.on = NO;
        [_discountView addSubview:_switchs];
        _distributionView=[[UIView alloc]initWithFrame:CGRectMake(0, _discountView.bottom, ScreenWidth, 120+155)];
        _distributionView.backgroundColor=[UIColor whiteColor];
        [self addSubview:_distributionView];
        _discountAmountLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, ScreenWidth-40, 45)];
        _discountAmountLabel.textColor=[UIColor blackColor];
        _discountAmountLabel.textAlignment = NSTextAlignmentRight;
        [_distributionView addSubview:_discountAmountLabel];
        UIView*gayView=[[UIView alloc]initWithFrame:CGRectMake(0, _discountAmountLabel.bottom, ScreenWidth, 10)];
        gayView.backgroundColor=zuiqianhuiColor;
        gayView.layer.borderWidth = 0.40;
        gayView.layer.borderColor = qianhuiColor.CGColor;
        [_distributionView addSubview:gayView];
        _noteTextView=[[UITextView alloc]init];
        _noteTextView.frame=CGRectMake(15, _discountAmountLabel.bottom+20, ScreenWidth-40, 90);
        _noteTextView.backgroundColor = [UIColor whiteColor];//设置它的背景颜色/Users/jiezsoft/Desktop/
        _noteTextView.scrollEnabled = YES;//是否可以拖动
        _noteTextView.delegate=self;
        _noteTextView.font = [UIFont fontWithName:@"Arial" size:15.0];//设置字体名字和字体大小
        _noteTextView.layer.borderWidth =0.0;
        CGColorSpaceRef registedSpace11 = CGColorSpaceCreateDeviceRGB();
        CGColorRef tureBTncolorref111 = CGColorCreate(registedSpace11,(CGFloat[]){ 0, 0, 0, 0.3 });
        [_noteTextView.layer setBorderColor:tureBTncolorref111];//边框颜色
        _noteTextView.layer.cornerRadius =0;
        [_distributionView addSubview:_noteTextView];
        im=[[UIImageView alloc]initWithFrame:CGRectMake(0, 5,18, 16)];
        im.image=[UIImage imageNamed:@"_0000_xingxifankui.png"];
        [_noteTextView addSubview:im];
        label=[[UILabel alloc]init];
        label.frame =CGRectMake(23, 7, ScreenWidth-60, 14);
        label.text = @"备注";
        label.textColor=qianhuiColor;
        label.numberOfLines=0;
        label.font=[UIFont systemFontOfSize:14];
//        label.enabled = NO;//lable必须设置为不可用
//        label.backgroundColor = [UIColor clearColor];
        [_noteTextView addSubview:label];
        _noteTextView.inputAccessoryView = myToolBar;
}
    return self;
}
-(void)setOrderExtFooterModel:(OrderExtFooterModel *)OrderExtFooterModel{
    
   [_verificationView removeFromSuperview];
    _verificationView=[[UIView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, 90)];
    _verificationView.backgroundColor=[UIColor whiteColor];
    [self addSubview:_verificationView];
    UIImageView *_xianview1 = [[UIImageView alloc]initWithFrame:CGRectMake(10,0, ScreenWidth-20, 0.5)];
    _xianview1.image=[UIImage imageNamed:@"hengxian"];
    [_verificationView addSubview:_xianview1];
    UILabel*verification=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 245, 45)];
    verification.text=@"验证码确认后方可使用";
    verification.font= [UIFont systemFontOfSize:15];
    [verification setTextColor:qianheiColor];
    [_verificationView addSubview:verification];
    _verificationCode = [[UITextField alloc] initWithFrame:CGRectMake(15, 45, 190, 44)];
    [_verificationCode setBorderStyle:UITextBorderStyleNone]; //外框类型
    _verificationCode.placeholder = @"请输入手机验证码"; //默认显示的
    _verificationCode.font = [UIFont systemFontOfSize: 14];
    _verificationCode.autocorrectionType = UITextAutocorrectionTypeNo;
    _verificationCode.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _verificationCode.returnKeyType = UIReturnKeyDone;
    _verificationCode.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    _verificationCode.delegate = self;
    _verificationCode.tag=6;
    _verificationCode.keyboardType = UIKeyboardTypeDecimalPad;
    [_verificationView addSubview:_verificationCode];
    _verificationCode.inputAccessoryView = myToolBar;
    ToObtain = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-90-15, 45+7.5, 90,30)];
    [ToObtain setTitle:@"获取验证码" forState:UIControlStateNormal];
    ToObtain.titleLabel.font = [UIFont systemFontOfSize: 14.0];
    [ToObtain.layer setCornerRadius:1.0]; //设置矩圆角半径
    [ToObtain.layer setBorderWidth:0.5];   //边框宽度
    CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){228/255.0, 228/255.0, 228/255.0, 1.0});
    [ToObtain.layer setBorderColor:tureBTncolorref];//边框颜色
    [ToObtain setTitleColor:[UIColor colorWithRed:249/255.0 green:53/255.0 blue:30/255.0 alpha:1.0]
                   forState:UIControlStateNormal];
    [ToObtain addTarget:self action:@selector(startTime) forControlEvents:UIControlEventTouchUpInside];
    [_verificationView addSubview:ToObtain];
    _discountView.frame=CGRectMake(0, 90, ScreenWidth, 50);
    if ([OrderExtFooterModel.phone isEqualToString:@"shsm"]) {
        _discountView.frame=CGRectMake(0, 90, ScreenWidth, 50);
        _addressView.frame=CGRectMake(0,_discountView.bottom, ScreenWidth, 175);
        _distributionView.frame=CGRectMake(0,_addressView.bottom, ScreenWidth, 120+155);
       
        
    }else{
        [_addressView removeFromSuperview];
        _distributionView.frame=CGRectMake(0,_discountView.bottom, ScreenWidth, 120+155);
    }
}
-(void)setOrderExtHeaderModel:(OrderExtHeaderModel *)OrderExtHeaderModel{
    [_addressView removeFromSuperview];
    _addressView=[[UIView alloc]initWithFrame:CGRectMake(0,_discountView.bottom, ScreenWidth, 175)];
    _addressView.backgroundColor=[UIColor whiteColor];
    [self addSubview:_addressView];
    
    _addOrEditor = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-80+12.5, 50/2-12.5, 50,25)];
    [_addOrEditor setTitleColor:shenhuiColor forState:UIControlStateNormal];
    [_addOrEditor.layer setCornerRadius:2];
    [_addOrEditor.layer setBorderWidth:0.5];
    [_addOrEditor.layer setMasksToBounds:YES];
    _addOrEditor .titleLabel.font=[UIFont systemFontOfSize:13];
    _addOrEditor.layer.borderColor=[[UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1] CGColor];
    [_addOrEditor setTitleColor:shenhuiColor  forState:UIControlStateNormal];
    [_addOrEditor addTarget:self action:@selector(chooseBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_addressView addSubview:_addOrEditor];

    UILabel*xx=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 145, 45)];
    xx.text=@"收货信息";
    xx.font= [UIFont systemFontOfSize:15];
    [xx setTextColor:qianheiColor];
    [_addressView addSubview:xx];
    UIImageView*line = [[UIImageView alloc]initWithFrame:CGRectMake(10,45, ScreenWidth-20, 0.5)];
    line.image=[UIImage imageNamed:@"hengxian"];
    [_addressView addSubview:line];
    UIImageView*line1 = [[UIImageView alloc]initWithFrame:CGRectMake(10,130, ScreenWidth-20, 0.5)];
    line1.image=[UIImage imageNamed:@"hengxian"];
    [_addressView addSubview:line1];
    NSString *s121 =self.consigneeName;
    UIFont *font121 = [UIFont systemFontOfSize:15];
    CGSize size121 = CGSizeMake(ScreenWidth-60, 2000);
    CGSize labelsize121 = [s121 sizeWithFont:font121 constrainedToSize:size121 lineBreakMode:NSLineBreakByCharWrapping];
    UILabel*Name=[[UILabel alloc]initWithFrame:CGRectMake(15, line.bottom+12.5,labelsize121.width, 15)];
    Name.text=self.consigneeName;
    Name.font= [UIFont systemFontOfSize:15];
    [Name setTextColor:qianheiColor];
    [_addressView addSubview:Name];
    UILabel*mendian1=[[UILabel alloc]initWithFrame:CGRectMake(Name.right+15,line.bottom+12.5, 200, 15)];
    mendian1.text=self.consigneePhone;
    mendian1.font= [UIFont systemFontOfSize:15];
    [mendian1 setTextColor:qianheiColor];
    [_addressView addSubview:mendian1];
    
    if ([self.address length ]>1) {
        [_addOrEditor setTitle:@"修改" forState:UIControlStateNormal];
    }else{
        [_addOrEditor setTitle:@"添加" forState:UIControlStateNormal];
            self.address=@"无地址可配送请添加地址！";
    }
    NSString *s1 =[NSString stringWithFormat:@"%@",  self.address];
    UIFont *f1 = [UIFont systemFontOfSize:contenfot];
    CGSize sizedizhi = CGSizeMake(ScreenWidth-33, 2000);
    CGSize labelsizedizhi = [s1 sizeWithFont:f1 constrainedToSize:sizedizhi lineBreakMode:NSLineBreakByCharWrapping];
    UILabel*addressLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, Name.bottom+7, ScreenWidth-30, labelsizedizhi.height)];
    addressLabel.text=[NSString stringWithFormat:@"%@",  self.address];
    addressLabel.textColor=shenhuiColor;
    addressLabel.numberOfLines=2;
    addressLabel.font=[UIFont systemFontOfSize:14];
    [_addressView addSubview:addressLabel];
    UILabel*expressFee=[[UILabel alloc]initWithFrame:CGRectMake(15,  130, 145,45)];
    expressFee.text=@"运费";
    expressFee.font= [UIFont systemFontOfSize:15];
    [expressFee setTextColor:qianheiColor];
    [_addressView addSubview:expressFee];
    
   _ExpressFee=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-200,  130, 175,45)];
    _ExpressFee.font= [UIFont systemFontOfSize:15];
    [_ExpressFee setTextColor:[UIColor clearColor]];
    _ExpressFee.textAlignment=NSTextAlignmentRight;
    [_addressView addSubview:_ExpressFee];
    _ExpressFee1=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-200,  130, 175,45)];
    _ExpressFee1.font= [UIFont systemFontOfSize:15];
    [_ExpressFee1 setTextColor:hongseColor];
    _ExpressFee1.textAlignment=NSTextAlignmentRight;
    [_addressView addSubview:_ExpressFee1];

    
    _courierFeesLabel=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-200,  130, 185,45)];
    _courierFeesLabel.font= [UIFont systemFontOfSize:15];
    [_courierFeesLabel setTextColor:hongseColor];
    _courierFeesLabel.textAlignment=NSTextAlignmentRight;
    [_addressView addSubview:_courierFeesLabel];
    UILabel *lineLabel12=[[UILabel alloc] initWithFrame:CGRectMake(10, 130+45-0.40, ScreenWidth-20, 0.40)];
    lineLabel12.backgroundColor=qianhuiColor;
    [_addressView addSubview:lineLabel12];
    _distributionView.frame=CGRectMake(0, _addressView.bottom, ScreenWidth, 120+155);
   
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:[user objectForKey:@"tokenId"] forKey:@"tokenId"];
    [params setValue:self.memberId forKey:@"memberId"];
    [params setValue:self.orderAmount forKey:@"orderAmount"];
    NSLog(@"%@",params);
    [HttpRequest requestData:@"Business/Order/ExpressFee" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
              NSLog(@"%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
              _ExpressFee.text=[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"expressFee"]];
                _ExpressFee1.text=[NSString stringWithFormat:@"¥ %@",[severDataDict.dataDict objectForKey:@"expressFee"]];
                [mDelegate how:@""];
            }else{
                [Global hideProgressHUDInternal:self];
            }}
        else
        {
            [Global hideProgressHUDInternal:self];
            [Global showTextHudSub:severDataDict.resultStr view:self];
        }
    }];

}
-(void)chooseBtn:(UIButton*)sender{
    [mDelegate chooesAddressBtn:@""];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    [mDelegate textViewEditing:@""];
    return self;
}
-(void)textViewDidChange:(UITextView *)textView
{
  if (textView.text.length == 0) {
        [im removeFromSuperview];
        im=[[UIImageView alloc]initWithFrame:CGRectMake(0, 5, 18, 16)];
        im.image=[UIImage imageNamed:@"_0000_xingxifankui.png"];
        [_noteTextView addSubview:im];
        label.text = @"备注";
        
    }else{
        [im removeFromSuperview];
        label.text = @"";
    }
}
-(void)OnTapDone{
    [mDelegate key:@""];

}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [mDelegate textFieldEditing:[NSString stringWithFormat:@"%ld", textField.tag]];
}
-(void)startTime{
    
//    if ([_phoneTextFied.text length]<1)
//    {
//        [self showAlertMessage:@"请输入手机号码"];
//    }else{
//        if ([_phoneTextFied.text length]!=11)
//        {
//            [self showAlertMessage:@"手机位数有误"];
//        }else{
    
            __block int timeout=60; //倒计时时间
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
            dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
            dispatch_source_set_event_handler(_timer, ^{
                if(timeout<=0){ //倒计时结束，关闭
                    dispatch_source_cancel(_timer);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //设置界面的按钮显示 根据自己需求设置
                        [ToObtain setTitle:@"获取" forState:UIControlStateNormal];
                        ToObtain.userInteractionEnabled = YES;
                    });
                }else{
                    //            int minutes = timeout / 60;
                    int seconds = timeout % 60;
                    NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //设置界面的按钮显示 根据自己需求设置
                        //                NSLog(@"____%@",strTime);
                        if (strTime.intValue==59) {
                            NSLog(@"____%@",strTime);
                            NSMutableDictionary *params = [NSMutableDictionary new];
                            [params setValue:self.memberPhone forKey:@"mobile"];
                            [params setValue:@"C" forKey:@"msgType"];
                              NSLog(@"====%@",params);
                            [HttpRequest requestData:@"System/SendMsg" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                                if (isSuccess) {
//                                    NSLog(@"====%@",severDataDict.dataDict);
                                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                                        [Global hideProgressHUDInternal:self];
                                            NSString*ResultString=[severDataDict.dataDict objectForKey:@"ResultString"];
                                            [Global showTextHudSub:ResultString view:self];
                                        
                                    }else{
                                        [Global hideProgressHUDInternal:self];
                                    }}
                                else
                                {
                                    [Global hideProgressHUDInternal:self];
                                    [Global showTextHudSub:severDataDict.resultStr view:self];
                                }
                            }];
                        }
                        
                        if (![strTime isEqualToString:@"00"]) {
                            [ToObtain setTitle:[NSString stringWithFormat:@"%@秒重获",strTime] forState:UIControlStateNormal];
                            
                        }
                        ToObtain.userInteractionEnabled = NO;
                        
                    });
                    timeout--;
                    
                }
            });
            dispatch_resume(_timer);
        }



@end
